<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Observer</title>
</head>
<body>
    <script>
        function observable(obj) {
            const queuedObservers = new Set();
            const handler = {
                set(target, key, value, receiver) {
                    if (key === 'add') {
                        return queuedObservers.add(value);
                    }
                    let result = Reflect.set(...arguments);
                    queuedObservers.forEach(observer => observer());
                    return result;
                }
            };
            return new Proxy(obj, handler);
        }


        let observe = observable({});
        
        observe.add = () => console.log('a');
        observe.add = () => console.log('b');
        observe.add = () => console.log('c');

        // 会触发所有observe.add赋值的函数
        observe.name = 10;
    </script>
</body>
</html>